<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">




<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/zhu/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/zhu/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/zhu/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/zhu/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/zhu/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/zhu/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/zhu/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="redis," />










<meta name="description" content="redis细粒度 https:&#x2F;&#x2F;www.cnblogs.com&#x2F;linjiqin&#x2F;archive&#x2F;2012&#x2F;04&#x2F;01&#x2F;2428695.html 设计数据库的三大范式  NoSQL特点   方便拓展（数据库之间没有关系）">
<meta property="og:type" content="article">
<meta property="og:title" content="redis(1)">
<meta property="og:url" content="https://gitee-zct.gitee.io/zhu/2021-04-18/redis/index.html">
<meta property="og:site_name" content="zhu">
<meta property="og:description" content="redis细粒度 https:&#x2F;&#x2F;www.cnblogs.com&#x2F;linjiqin&#x2F;archive&#x2F;2012&#x2F;04&#x2F;01&#x2F;2428695.html 设计数据库的三大范式  NoSQL特点   方便拓展（数据库之间没有关系）">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/gitee-zct/os/raw/master/pic/20210411-203918-0364.png">
<meta property="og:image" content="https://gitee.com/gitee-zct/os/raw/master/pic/20210416-215459-0904.png">
<meta property="article:published_time" content="2021-04-17T16:55:59.038Z">
<meta property="article:modified_time" content="2021-04-17T16:57:45.327Z">
<meta property="article:author" content="zhu">
<meta property="article:tag" content="redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/gitee-zct/os/raw/master/pic/20210411-203918-0364.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee-zct.gitee.io/zhu/2021-04-18/redis/"/>





  <title>redis(1) | zhu</title>
  








<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>


    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/zhu/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">zhu</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">我的小世界</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/zhu/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/zhu/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/zhu/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/zhu/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/zhu/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/zhu/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://gitee-zct.gitee.io/zhu/zhu/2021-04-18/redis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/zhu/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zhu">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">redis(1)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-04-18T00:55:59+08:00">
                2021-04-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/zhu/categories/%E5%AD%A6%E4%B9%A0/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  8.4k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  42
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="redis"><a href="#redis" class="headerlink" title="redis"></a>redis</h1><p>细粒度</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/linjiqin/archive/2012/04/01/2428695.html">https://www.cnblogs.com/linjiqin/archive/2012/04/01/2428695.html</a></p>
<p>设计数据库的三大范式</p>
<blockquote>
<p>NoSQL特点</p>
</blockquote>
<ol>
<li>方便拓展（数据库之间没有关系）</li>
</ol>
<p><em><span id="more"></span></em></p>
<blockquote>
<p>Redis能干吗</p>
</blockquote>
<ol>
<li><p>内存存储，持久化，内存中是断电即失的所以持久化很重要（rdb,aof）</p>
</li>
<li><p>效率高，可以用于高速缓存</p>
</li>
<li><p>发布订阅系统</p>
</li>
<li><p>地图信息分析</p>
</li>
<li><p>计时器，计数器（浏览量）</p>
</li>
<li><p>。。。</p>
</li>
</ol>
<blockquote>
<p>特性</p>
</blockquote>
<ol>
<li>多样 的数据类型</li>
<li>持久化</li>
<li>集群</li>
<li>事物</li>
<li>..</li>
</ol>
<blockquote>
<p>学习中需要用到的东西</p>
</blockquote>
<ol>
<li><a target="_blank" rel="noopener" href="https://redis.io/">https://redis.io</a></li>
<li><a target="_blank" rel="noopener" href="http://www.redis.cn/">http://www.redis.cn</a></li>
<li>windows在github下载（停更很久了，推荐在linux服务器搭建）</li>
</ol>
<blockquote>
<p>安装</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ wget https:&#x2F;&#x2F;download.redis.io&#x2F;releases&#x2F;redis-6.2.1.tar.gz</span><br><span class="line">$ tar xzf redis-6.2.1.tar.gz</span><br><span class="line">$ cd redis-6.2.1</span><br><span class="line">$ make</span><br></pre></td></tr></table></figure>

<p>在    /root</p>
<p>进入解压后的文件可以看到redis的配置文件</p>
<p><img src="https://gitee.com/gitee-zct/os/raw/master/pic/20210411-203918-0364.png" alt="image-20210411201823740"></p>
<ol start="2">
<li>```<br>基本环境安装<br>安装gcc-c++<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-202420-0846.png)</span><br><span class="line"></span><br><span class="line">确认一下</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">make</span><br><span class="line">make install</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">5. redis 默认安装环境</span><br><span class="line"></span><br><span class="line">   ~~~</span><br><span class="line">   &#x2F;usr&#x2F;local&#x2F;bin</span><br><span class="line">   ~~~</span><br><span class="line"></span><br><span class="line">6. 将redis配置文件(&#x2F;root&#x2F;redi..6.8&#x2F;redis.conf)复制到目录(&#x2F;usr&#x2F;local&#x2F;bin&#x2F;kconfig)下,之后就使用这个备份的进行启动</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-203733-0098.png)</span><br><span class="line"></span><br><span class="line">7. redis默认不是后台启动的,要修改配置文件![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-202640-0088.png)</span><br><span class="line"></span><br><span class="line">   </span><br><span class="line"></span><br><span class="line">8. 启动redis服务</span><br><span class="line"></span><br><span class="line">   ![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-200843-0130.png)</span><br><span class="line"></span><br><span class="line">redis后台启动没有输出,输入ps -ef可以验证</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-205645-0805.png)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">1. ![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-202947-0989.png)</span><br><span class="line"></span><br><span class="line">使用redis客户端测试连接,测试set,测试get ,查看所有的键</span><br><span class="line"></span><br><span class="line">11.关闭redis服务器命令:</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"># shutdown</span><br><span class="line"># exit</span><br><span class="line"># ps -ef | grep redis</span><br><span class="line">root     14445 14389  0 20:56 pts&#x2F;1    00:00:00 grep --color&#x3D;auto redis</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">&lt;img src&#x3D;&quot;https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-213302-0054.png&quot; style&#x3D;&quot;zoom: 67%;&quot; &#x2F;&gt;</span><br><span class="line"></span><br><span class="line">后面使用单机多redis启动集群</span><br><span class="line"></span><br><span class="line">---</span><br><span class="line"></span><br><span class="line">## 测试性能</span><br><span class="line"></span><br><span class="line">redis-benchmark是一个压力测试工具</span><br><span class="line"></span><br><span class="line">官方自带的性能测试工具</span><br><span class="line"></span><br><span class="line">&lt;img src&#x3D;&quot;https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-215303-0143.png&quot; style&#x3D;&quot;zoom:67%;&quot; &#x2F;&gt;</span><br><span class="line"></span><br><span class="line">~~~ </span><br><span class="line"># 测试:100个并发连接 10000个请求</span><br><span class="line">redis-benchmark -h localhost -p 6379 -c 100 -n 10000</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">如何查看分析![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-212612-0914.png)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">---</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">## 基础知识</span><br><span class="line"></span><br><span class="line">redis默认16个数据库</span><br><span class="line"></span><br><span class="line">默认使用第0个</span><br><span class="line"></span><br><span class="line">可以使用select切换</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">[root@izbp15hyph4achwhnnv8i5z kconfig]# redis-cli</span><br><span class="line">127.0.0.1:6379&gt; select 3  #切换数据库</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[3]&gt; dbsize #查看大小</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379[3]&gt; </span><br><span class="line">~~~</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li>0.0.1:6379&gt; select 3<br>OK</li>
<li>0.0.1:6379[3]&gt; dbsize<br>(integer) 0</li>
<li>0.0.1:6379[3]&gt; set name zhu<br>OK</li>
<li>0.0.1:6379[3]&gt; dbsize<br>(integer) 1</li>
<li>0.0.1:6379[3]&gt; select 7<br>OK</li>
<li>0.0.1:6379[7]&gt; get name<br>(nil)</li>
<li>0.0.1:6379[7]&gt; select 3<br>OK</li>
<li>0.0.1:6379[3]&gt; get name<br>“zhu”</li>
<li>0.0.1:6379[3]&gt; </li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">keys * 查看数据库所有的Key</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">127.0.0.1:6379[3]&gt; keys *</span><br><span class="line">1) &quot;age&quot;</span><br><span class="line">2) &quot;name&quot;</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">清空当前数据库</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">127.0.0.1:6379[3]&gt; flushdb</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[3]&gt; keys *</span><br><span class="line">(empty array)</span><br><span class="line">127.0.0.1:6379[3]&gt; </span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">清空全部数据库的内容 flushall</span><br><span class="line"></span><br><span class="line">---</span><br><span class="line"></span><br><span class="line">&gt; redis是单线程的</span><br><span class="line"></span><br><span class="line">redis是基于内存操作的,cpu不是redis的性能瓶颈,redis的瓶颈是机器的**内存和网络带宽**,既然可以使用单线程实现,就用单线程了</span><br><span class="line"></span><br><span class="line">redis是c语言写的,官方数据 10 0000 0000+ 的QPS ,完全不比同样使用Key-value Memcache差</span><br><span class="line"></span><br><span class="line">&gt; Redis为什么单线程还这么快</span><br><span class="line"></span><br><span class="line">1. 误区1:高性能的服务器一定是多线程的</span><br><span class="line">2. 误区2:多线程(CPU上下文切换)一定比单线程效率高</span><br><span class="line"></span><br><span class="line">核心:</span><br><span class="line"></span><br><span class="line">- redis是将所有的数据全都放在内存中的,所以说使用单线程去操作就是效率最高的,:多线程(CPU上下文切换:耗时1500nm-2000纳秒之间),对于内存系统来说,如果没有上下文切换,效率就是好.多次读写都是一个cpu,在内存下是最优方案.</span><br><span class="line"></span><br><span class="line"># 五大数据类型</span><br><span class="line"></span><br><span class="line">----</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Redis 是一个开源（BSD许可）的，内存中的数据结构存储系统，它可以用作&#x3D;&#x3D;数据库、缓存和消息中间件MQ。&#x3D;&#x3D;</span><br><span class="line"></span><br><span class="line">它支持多种类型的数据结构，如 字符串（strings）， 散列（hashes）， 列表（lists）， 集合</span><br><span class="line">（sets）， 有序集合（sorted sets） 与范围查询， bitmaps， hyperloglogs 和 地理空间（geospatial） 索引半径查询。 </span><br><span class="line"></span><br><span class="line">Redis 内置了 复制（replication），LUA脚本（Lua scripting）， LRU驱动事件（LRU eviction），事务（transactions） 和不同级别的 磁盘持久化（persistence）， 并通过Redis哨兵（Sentinel）和自动 分区（Cluster）提供高可用性（high availability）。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Redis_Key</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127 .0.0.1:6379&gt; keys *  # 查看所有的key</span><br><span class="line">(empty list or set)</span><br><span class="line">127 .0.0.1:6379&gt; set name lisen  # set key</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379&gt; keys *</span><br><span class="line">1 ) &quot;name&quot;</span><br><span class="line">127 .0.0.1:6379&gt; set age 1</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379&gt; keys *</span><br><span class="line">1 ) &quot;age&quot;</span><br><span class="line">2 ) &quot;name&quot;</span><br><span class="line">127 .0.0.1:6379&gt; EXISTS name  # 判断当前的key是否存在</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; EXISTS name</span><br><span class="line">(integer) 0</span><br><span class="line">127 .0.0.1:6379&gt; move name 1 # 移除当前的key</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; keys *</span><br><span class="line">1 ) &quot;age&quot;</span><br><span class="line">127 .0.0.1:6379&gt; set name lisen</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379&gt; keys *</span><br><span class="line">1 ) &quot;age&quot;</span><br><span class="line">2 ) &quot;name&quot;</span><br><span class="line">127 .0.0.1:6379&gt; clear</span><br><span class="line">127 .0.0.1:6379&gt; keys *</span><br><span class="line">1 ) &quot;age&quot;</span><br><span class="line">2 ) &quot;name&quot;</span><br><span class="line">127 .0.0.1:6379&gt; get name</span><br><span class="line">&quot;qinjiang&quot;</span><br><span class="line">127 .0.0.1:6379&gt; EXPIRE name 10 #设置key的过期时间，单位是秒</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; ttl name  # 查看当前key的剩余时间</span><br><span class="line">(integer) 4</span><br><span class="line">127 .0.0.1:6379&gt; ttl name</span><br><span class="line">(integer) 3</span><br><span class="line">127 .0.0.1:6379&gt; ttl name</span><br><span class="line">(integer) 2</span><br><span class="line">127 .0.0.1:6379&gt; ttl name</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; ttl name</span><br><span class="line">(integer) -</span><br><span class="line">127 .0.0.1:6379&gt; get name # 就会自动删除 keys *也就没了</span><br><span class="line">(nil)</span><br><span class="line">127 .0.0.1:6379&gt; type name  # 查看当前key的一个类型！string</span><br><span class="line">127 .0.0.1:6379&gt; type age   # string</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">## string类型</span><br><span class="line"></span><br><span class="line">----</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">############################################################</span><br><span class="line">127 .0.0.1:6379&gt; set name lisen  # 设置值</span><br><span class="line">127 .0.0.1:6379&gt; get lisen # 获得值</span><br><span class="line">127 .0.0.1:6379&gt; keys * # 获得所有的key</span><br><span class="line">127 .0.0.1:6379&gt; EXISTS lisen  # 判断某一个key是否存在</span><br><span class="line">(integer) 1</span><br><span class="line">#追加字符串，如果当前key不存在，就相当于setkey</span><br><span class="line">127 .0.0.1:6379&gt; APPEND lisen &quot;hehe&quot;  # 9就是name的值的长度</span><br><span class="line">(integer) 9</span><br><span class="line">127 .0.0.1:6379&gt; get lisen</span><br><span class="line">&quot;lisenhehe&quot;</span><br><span class="line">127 .0.0.1:6379&gt; STRLEN key1  # 获取字符串的长度！</span><br><span class="line">(integer) 9</span><br><span class="line">############################################################</span><br><span class="line"># i++</span><br><span class="line"># 步长 i+&#x3D;</span><br><span class="line">127 .0.0.1:6379&gt; set views 0 # 初始浏览量为 0</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379&gt; get views</span><br><span class="line">&quot;0&quot;</span><br><span class="line">127 .0.0.1:6379&gt; incr views  # 自增 1 浏览量变为 1</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; incr views</span><br><span class="line">(integer) 2</span><br><span class="line">127 .0.0.1:6379&gt; get viewsg</span><br><span class="line">&quot;2&quot;</span><br><span class="line">127 .0.0.1:6379&gt; decr views  # 自减 1 浏览量-1</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; decr views</span><br><span class="line">(integer) 0</span><br><span class="line">127 .0.0.1:6379&gt; decr views</span><br><span class="line">(integer) -1</span><br><span class="line">127 .0.0.1:6379&gt; get views</span><br><span class="line">&quot;-1&quot;</span><br><span class="line">127 .0.0.1:6379&gt; INCRBY views 10 # 可以设置步长，指定增量！</span><br><span class="line">(integer) 9</span><br><span class="line">127 .0.0.1:6379&gt; INCRBY views 10</span><br><span class="line">(integer) 19</span><br><span class="line">127 .0.0.1:6379&gt; DECRBY views 5</span><br><span class="line">(integer) 14</span><br><span class="line">############################################################</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">### 字符串范围range</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127.0.0.1:6379[13]&gt; set key1 hello,lisen # 设置 key1 的值</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[13]&gt; GETRANGE key1 0 3  # 截取字符串 [0,3]</span><br><span class="line">&quot;hell&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; GETRANGE key1 0 -1 # 获取全部的字符串 和 get key是一样的</span><br><span class="line">&quot;hello,lisen&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; SETRANGE key1 1 xx #替换指定位置开始的字符串！写多少长度的就换多少长度的</span><br><span class="line">127.0.0.1:6379[13]&gt; SETRANGE key1 0 l</span><br><span class="line">(integer) 11</span><br><span class="line">127.0.0.1:6379[13]&gt; get key1</span><br><span class="line">&quot;lello,lisen&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; SETRANGE key1 0 lllllll</span><br><span class="line">(integer) 11</span><br><span class="line">127.0.0.1:6379[13]&gt; get key1</span><br><span class="line">&quot;lllllllisen&quot;</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">### 设置set的过期时间</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">setex (set with expire) # 设置过期时间</span><br><span class="line">setnx (set if not exist) # 不存在再添加（在分布式锁中会常常使用！）</span><br><span class="line">ttl key # 查看过期时间</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379[13]&gt; SETEX key1 10 lisen # key1 的值为 lisen,10秒后过期</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379[13]&gt; setnx mykey &quot;redis&quot; # 如果mykey 不存在，创建mykey</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line"># 同时设置多个值</span><br><span class="line"># mset</span><br><span class="line">127.0.0.1:6379[13]&gt; mset k1 v1 k2 v2 k3 v3</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[13]&gt; keys *</span><br><span class="line">1) &quot;view&quot;</span><br><span class="line">2) &quot;k3&quot;</span><br><span class="line">3) &quot;k2&quot;</span><br><span class="line">4) &quot;k1&quot;</span><br><span class="line"></span><br><span class="line"># 同时获取多个值</span><br><span class="line"># mget</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127 .0.0.1:6379&gt; mget k1 k2 k3 </span><br><span class="line">1 ) &quot;v1&quot;</span><br><span class="line">2 ) &quot;v2&quot;</span><br><span class="line">3 ) &quot;v3&quot;</span><br><span class="line">127.0.0.1:6379&gt; msetnx k1 v1 k4 v4   #msetnx 是一个原子性操作,要么一起成功,要么一起失败mult set if not exists</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; keys *</span><br><span class="line">1) &quot;k2&quot;</span><br><span class="line">2) &quot;k1&quot;</span><br><span class="line">3) &quot;k3&quot;</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127 .0.0.1:6379&gt; msetnx k1 v1 k4 v4  # msetnx 是一个原子性的操作，要么一起成功，要么一起失败！</span><br><span class="line"></span><br><span class="line"># 对象</span><br><span class="line">set user:1 &#123;name:zhangsan,age:3&#125;  # 设置一个user:1 对象 值为 json字符来保存一个对象！</span><br><span class="line"></span><br><span class="line"># 这里的key是一个巧妙的设计： user:&#123;id&#125;:&#123;filed&#125; , 如此设计在Redis中是完全OK了！</span><br><span class="line">127 .0.0.1:6379&gt; mset user:1:name zhangsan user:1:age 2</span><br><span class="line">OK</span><br><span class="line">127 .0.0.1:6379&gt; mget user:1:name user:1:age</span><br><span class="line">1 ) &quot;zhangsan&quot;</span><br><span class="line">2 ) &quot;2&quot;</span><br><span class="line"></span><br><span class="line">##########################################################################</span><br><span class="line">getset # 先get然后在set</span><br><span class="line"></span><br><span class="line">127 .0.0.1:6379&gt; getset db redis # 如果不存在值，则返回 nil</span><br><span class="line">(nil)</span><br><span class="line">127 .0.0.1:6379&gt; get db</span><br><span class="line">&quot;redis</span><br><span class="line">127 .0.0.1:6379&gt; getset db mongodb  # 如果存在值，获取原来的值，并设置新的值</span><br><span class="line">&quot;redis&quot;</span><br><span class="line">127 .0.0.1:6379&gt; get db</span><br><span class="line">&quot;mongodb&quot;</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">## List(列表)</span><br><span class="line"></span><br><span class="line">### 基本的数据类型，列表</span><br><span class="line"></span><br><span class="line">在redis里面，我们可以把list玩成 ，栈、队列、阻塞队列！</span><br><span class="line"></span><br><span class="line">所有的list命令都是用l开头的，Redis不区分大小命令</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">##########################################################################</span><br><span class="line"># LPUSH</span><br><span class="line"># RPUSH</span><br><span class="line"># 这边只能左边插入右边插入 但是取值只能LRANGE</span><br><span class="line">127 .0.0.1:6379&gt; LPUSH list one  # 将一个值或者多个值，插入到列表头部 （左）</span><br><span class="line">(integer) 1</span><br><span class="line">127 .0.0.1:6379&gt; LPUSH list two</span><br><span class="line">(integer) 2</span><br><span class="line">127 .0.0.1:6379&gt; LPUSH list three</span><br><span class="line">(integer) 3</span><br><span class="line">127 .0.0.1:6379&gt; LRANGE list 0 -1 # 获取list中值！</span><br><span class="line">1 ) &quot;three&quot;</span><br><span class="line">2 ) &quot;two&quot;</span><br><span class="line">3 ) &quot;one&quot;</span><br><span class="line">127 .0.0.1:6379&gt; LRANGE list 0 1 # 通过区间获取具体的值！</span><br><span class="line">1 ) &quot;three&quot;</span><br><span class="line">2 ) &quot;two&quot;</span><br><span class="line">127 .0.0.1:6379&gt; Rpush list righr  # 将一个值或者多个值，插入到列表位部 （右）</span><br><span class="line">(integer) 4</span><br><span class="line">127 .0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1 ) &quot;three&quot;</span><br><span class="line">2 ) &quot;two&quot;</span><br><span class="line">3 ) &quot;one&quot;</span><br><span class="line">4 ) &quot;righr&quot;</span><br><span class="line">##########################################################################</span><br><span class="line"># 双端队列的感觉</span><br><span class="line"># LPOP</span><br><span class="line"># RPOP</span><br><span class="line">127 .0.0.1:6379&gt; Lpop list  # 移除list的第一个元素 </span><br><span class="line">&quot;three&quot;</span><br><span class="line">127 .0.0.1:6379&gt; Rpop list  # 移除list的最后一个元素</span><br><span class="line">&quot;righr&quot;</span><br><span class="line">127 .0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1 ) &quot;two&quot;</span><br><span class="line">2 ) &quot;one&quot;</span><br><span class="line">##########################################################################</span><br><span class="line"># Lindex</span><br><span class="line">127 .0.0.1:6379&gt; LRANGE list 0 -1</span><br><span class="line">1 ) &quot;two&quot;</span><br><span class="line">2 ) &quot;one&quot;</span><br><span class="line">127 .0.0.1:6379&gt; lindex list 1 # 通过下标获得 list 中的某一个值！</span><br><span class="line">&quot;one&quot;</span><br><span class="line">127 .0.0.1:6379&gt; lindex list 0</span><br><span class="line">&quot;two&quot;</span><br><span class="line">##########################################################################</span><br><span class="line"># Llen key  # list的长度</span><br><span class="line">127 .0.0.1:6379&gt; Llen list # 返回列表的长度</span><br><span class="line">(integer) 3</span><br><span class="line">##########################################################################</span><br><span class="line"># 移除指定的值！</span><br><span class="line"># lrem key 个数 具体的值 # 移除指定的值 可移除多个</span><br><span class="line">127 .0.0.1:6379&gt; lrem list 1 one # 移除list集合中指定个数的value，精确匹配</span><br><span class="line">##########################################################################</span><br><span class="line"># ltrim 修剪 没有rtrim</span><br><span class="line">127.0.0.1:6379[13]&gt; rpush mylist hello hello1 hello2 hello3</span><br><span class="line">(integer) 4</span><br><span class="line">127.0.0.1:6379[13]&gt; LRANGE mylist 0 -1</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;hello1&quot;</span><br><span class="line">3) &quot;hello2&quot;</span><br><span class="line">4) &quot;hello3&quot;</span><br><span class="line"># 通过下标截取指定的长度，这个list已经被改变了，截断了只剩下截取的元素！</span><br><span class="line">127.0.0.1:6379[13]&gt; LTRIM mylist 1 2</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379[13]&gt; LRANGE mylist 0 -1</span><br><span class="line">1) &quot;hello1&quot;</span><br><span class="line">2) &quot;hello2&quot;</span><br><span class="line"></span><br><span class="line">##########################################################################</span><br><span class="line"># 移除列表的最后一个元素，将他移动到新的列表中！</span><br><span class="line"># RPOPLPUSH source destination</span><br><span class="line"># rpoplpush list名称 newlist名称 </span><br><span class="line">127.0.0.1:6379[13]&gt; LRANGE mylist 0 -1</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;hello1&quot;</span><br><span class="line">3) &quot;hello2&quot;</span><br><span class="line">4) &quot;hello3&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; RPOPLPUSH mylist mylistnew</span><br><span class="line">&quot;hello3&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; LRANGE mylist 0 -1</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;hello1&quot;</span><br><span class="line">3) &quot;hello2&quot;</span><br><span class="line">127.0.0.1:6379[13]&gt; LRANGE mylistnew 0 -1</span><br><span class="line">1) &quot;hello3&quot;</span><br><span class="line"># todo</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210411-234306-0204.png)</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127.0.0.1:6379&gt; lrange list 0 -1</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;a&quot;</span><br><span class="line">3) &quot;hello2&quot;</span><br><span class="line">4) &quot;hello&quot;</span><br><span class="line">127.0.0.1:6379&gt; linsert list before a b #左插入,将某个具体的value插入列表中某个value的前面或后面</span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:6379&gt; lrange list 0 -1  </span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;b&quot;</span><br><span class="line">3) &quot;a&quot;</span><br><span class="line">4) &quot;hello2&quot;</span><br><span class="line">5) &quot;hello&quot;</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">list实际上是一个链表，before Node after , left, right 都可以插入值</span><br><span class="line"></span><br><span class="line">如果key不存在，则创建新的链表</span><br><span class="line"></span><br><span class="line">如果key存在，新增内容</span><br><span class="line"></span><br><span class="line">如果移除了所有值，空链表，也代表不存在</span><br><span class="line"></span><br><span class="line">在两边插入或者改动值，效率最高！修改中间元素，效率相对较低</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">应用：</span><br><span class="line"></span><br><span class="line">&#x3D;&#x3D;消息排队！消息队列（Lpush Rpop）,栈（Lpush Lpop）&#x3D;&#x3D;</span><br><span class="line"></span><br><span class="line">## Set(集合)</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">Redis的Set是string类型的无序集合。集合成员是唯一的，这就意味着集合中不能出现重复的数据。</span><br><span class="line"></span><br><span class="line">Redis 中 集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是O(1)。</span><br><span class="line"></span><br><span class="line">集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">[root@izbp15hyph4achwhnnv8i5z ~]# redis-cli</span><br><span class="line">127.0.0.1:6379&gt; </span><br><span class="line">127.0.0.1:6379&gt; sadd myset &quot;hello&quot;  # set值</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset &quot;hello2&quot;  </span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers myset   #查看指定set的所有值</span><br><span class="line">1) &quot;hello2&quot;</span><br><span class="line">2) &quot;hello&quot; </span><br><span class="line">127.0.0.1:6379&gt; Sismember myset hello  #判断某个值是不是在set中</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; Sismember myset world  </span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; scard myset         #获取set集合中的内容元素个数</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; srem myset hello    #移除set集合中的指定元素</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; scard myset   </span><br><span class="line">(integer) 1</span><br><span class="line">##################################################################</span><br><span class="line">set 无序不重复集合。抽随机</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; srandmember myset  #随机抽选处一个元素</span><br><span class="line">&quot;zhu&quot;</span><br><span class="line">127.0.0.1:6379&gt; srandmember myset  </span><br><span class="line">&quot;hello2&quot;</span><br><span class="line">127.0.0.1:6379&gt; srandmember myset 2 #随机抽取指定个数元素</span><br><span class="line">1) &quot;hello2&quot;</span><br><span class="line">2) &quot;zhu&quot;</span><br><span class="line">##################################################################</span><br><span class="line">删除指定的key、随机删除key</span><br><span class="line">127.0.0.1:6379&gt; spop myset</span><br><span class="line">&quot;hello2&quot;</span><br><span class="line">127.0.0.1:6379&gt; smembers myset</span><br><span class="line">1) &quot;zhu&quot;</span><br><span class="line">2) &quot;wang&quot;</span><br><span class="line">##################################################################</span><br><span class="line">将一个指定的k</span><br><span class="line">127.0.0.1:6379&gt; sadd myset hello</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset world</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset zhu</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd myset2 koko</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smove myset myset2 hello #将一个指定的值移动到另一个set集合</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; smembers myset</span><br><span class="line">1) &quot;zhu&quot;</span><br><span class="line">2) &quot;world&quot;</span><br><span class="line">127.0.0.1:6379&gt; smembers myset2</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;koko&quot;</span><br><span class="line">##################################################################</span><br><span class="line">微博，共同关注，（并集）</span><br><span class="line">数字集合类：</span><br><span class="line">- 差集</span><br><span class="line">- 交集</span><br><span class="line">- 并集</span><br><span class="line">127.0.0.1:6379&gt; sadd key1 a</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key1 b</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key1 c</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 c</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 d</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sadd key2 e</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; sdiff key1 key2  #差集</span><br><span class="line">1) &quot;a&quot;</span><br><span class="line">2) &quot;b&quot;</span><br><span class="line">127.0.0.1:6379&gt; sinter key1 key2 #交集</span><br><span class="line">1) &quot;c&quot;</span><br><span class="line">127.0.0.1:6379&gt; sunion key1 key2 #并集</span><br><span class="line">1) &quot;e&quot;</span><br><span class="line">2) &quot;c&quot;</span><br><span class="line">3) &quot;a&quot;</span><br><span class="line">4) &quot;b&quot;</span><br><span class="line">5) &quot;d&quot;</span><br><span class="line"></span><br><span class="line">微博，A用户将所有关注的人放在一个set集合中，将他的粉丝返回给在一个集合中，</span><br><span class="line">共同关注，共同爱好，二度好友（六度分割理论）</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">## Hash</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line">Map集合，key-map!这时候这个值就是map集合,本质和string类型没有太大的区别，还是一个简单的key-value</span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">set myhash filed zhu </span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127.0.0.1:6379&gt; hset myhash field1 zhu    #set一个具体的key-value</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hget myhash field1 </span><br><span class="line">&quot;zhu&quot;</span><br><span class="line">127.0.0.1:6379&gt; hmset myhash field1 hello field2 world  #set多个key-value</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hmget myhash field1 field2      #获取多个字段值</span><br><span class="line">1) &quot;hello&quot;</span><br><span class="line">2) &quot;world&quot;</span><br><span class="line">127.0.0.1:6379&gt; hgetall myhash          #获取全部数据</span><br><span class="line">1) &quot;field1&quot;</span><br><span class="line">2) &quot;hello&quot;</span><br><span class="line">3) &quot;field2&quot;</span><br><span class="line">4) &quot;world&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hdel myhash field1  #删除hash指定的key字段，对应的value值也就消失了</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hgetall myhash</span><br><span class="line">1) &quot;field2&quot;</span><br><span class="line">2) &quot;world&quot;</span><br><span class="line">#####################################################################</span><br><span class="line">hlen  </span><br><span class="line">127.0.0.1:6379&gt; hmset myhash field1 hello field2 world</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; hgetall myhash</span><br><span class="line">1) &quot;field2&quot;</span><br><span class="line">2) &quot;world&quot;</span><br><span class="line">3) &quot;field1&quot;</span><br><span class="line">4) &quot;hello&quot;</span><br><span class="line">127.0.0.1:6379&gt; hlen myhash  #获取hash表的字段数量</span><br><span class="line">(integer) 2</span><br><span class="line">#####################################################################</span><br><span class="line">127.0.0.1:6379&gt; hexists myhash field1        #判断指定hash中指定字段是否存在</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hexists myhash field3</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; hkeys myhash          #获取所有字段</span><br><span class="line">1) &quot;field2&quot;</span><br><span class="line">2) &quot;field1&quot;</span><br><span class="line">127.0.0.1:6379&gt; hvals myhash       #获取所有的值</span><br><span class="line">1) &quot;world&quot;</span><br><span class="line">2) &quot;hello&quot;</span><br><span class="line">#####################################################################</span><br><span class="line">127.0.0.1:6379&gt; hset myhash field3 5</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; Hincrby myhash field3 1 #指定增量</span><br><span class="line">(integer) 6</span><br><span class="line">127.0.0.1:6379&gt; Hincrby myhash field3 -1  </span><br><span class="line">(integer) 5</span><br><span class="line">127.0.0.1:6379&gt; hsetnx myhash field4 hello #如果存在则可以设置</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; hsetnx myhash field4 world #如果不存在则不能设置</span><br><span class="line">(integer) 0</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">hash变更的数据 user name age ，尤其是用户信息之类的，hash更适合存对象的存储，String更加是恶化字符串存储！</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>127.0.0.1:6379&gt; hset user:1 name zhu<br>(integer) 1<br>127.0.0.1:6379&gt; hget user:1 name<br>“zhu”</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## Zset（有序集合）底层是跳跃链表</span><br><span class="line"></span><br><span class="line">---</span><br><span class="line"></span><br><span class="line">在set的基础上加了一个值,set k1 v1      zset k1 score v1</span><br><span class="line"></span><br><span class="line">1 2 </span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">127.0.0.1:6379&gt; zadd myset 1 one</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd myset 2 two 3 three    #添加一个值</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; zrange myset 0 -1           #添加多个值</span><br><span class="line">1) &quot;one&quot;</span><br><span class="line">2) &quot;two&quot;</span><br><span class="line">3) &quot;three&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zadd salary 2500 zhu</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd salary 250 wang</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zadd salary 4000 zhang</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore salary -inf +inf #从小到大排序</span><br><span class="line">1) &quot;wang&quot;</span><br><span class="line">2) &quot;zhu&quot;</span><br><span class="line">3) &quot;zhang&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrevrange salary 0 -1  #从大到小进行排序</span><br><span class="line">1) &quot;zhang&quot;</span><br><span class="line">2) &quot;wang&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zrangebyscore salary -inf 3000  #小于3000的</span><br><span class="line">1) &quot;wang&quot;</span><br><span class="line">2) &quot;zhu&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrevrangebyscore salary +inf -inf  # zrevrange 最大到最小</span><br><span class="line">1) &quot;zhang&quot;</span><br><span class="line">2) &quot;zhu&quot;</span><br><span class="line">3) &quot;wang&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrevrangebyscore salary +inf -inf withscores # 显示全部的用户并附带值</span><br><span class="line">1) &quot;zhang&quot;</span><br><span class="line">2) &quot;4000&quot;</span><br><span class="line">3) &quot;zhu&quot;</span><br><span class="line">4) &quot;2500&quot;</span><br><span class="line">5) &quot;wang&quot;</span><br><span class="line">6) &quot;250&quot;</span><br><span class="line">#####################################################################</span><br><span class="line">移除rem中的元素</span><br><span class="line">127.0.0.1:6379&gt; zrange salary 0 -1</span><br><span class="line">1) &quot;wang&quot;</span><br><span class="line">2) &quot;zhu&quot;</span><br><span class="line">3) &quot;zhang&quot;</span><br><span class="line">127.0.0.1:6379&gt; zrem salary zhu       # 移除有序集合的指定元素</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; zrange salary 0 -1</span><br><span class="line">1) &quot;wang&quot;</span><br><span class="line">2) &quot;zhang&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; zcard salary   # 获取有序集合中的个数</span><br><span class="line">(integer) 2</span><br><span class="line">###################################################################</span><br><span class="line">127.0.0.1:6379&gt; zadd myset 1 hello 2 world 3 zhu  </span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zcount myset 1 3     #获取指定区间成员数量</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; zcount myset 1 2</span><br><span class="line">(integer) 2</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">~~~js</span><br><span class="line">-------------------ZADD--ZCARD--ZCOUNT--------------</span><br><span class="line">127.0.0.1:6379&gt; ZADD myzset 1 m1 2 m2 3 m3 # 向有序集合myzset中添加成员m1 score&#x3D;1 以及成员m2 score&#x3D;2..</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; ZCARD myzset # 获取有序集合的成员数</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myzset 0 1 # 获取score在 [0,1]区间的成员数量</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; ZCOUNT myzset 0 2</span><br><span class="line">(integer) 2</span><br><span class="line">----------------ZINCRBY--ZSCORE--------------------------</span><br><span class="line">127.0.0.1:6379&gt; ZINCRBY myzset 5 m2 # 将成员m2的score +5</span><br><span class="line">&quot;7&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZSCORE myzset m1 # 获取成员m1的score</span><br><span class="line">&quot;1&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZSCORE myzset m2</span><br><span class="line">&quot;7&quot;</span><br><span class="line"></span><br><span class="line">--------------ZRANK--ZRANGE-----------------------------------</span><br><span class="line">127.0.0.1:6379&gt; ZRANK myzset m1 # 获取成员m1的索引，索引按照score排序，score相同索引值按字典顺序顺序增加</span><br><span class="line">(integer) 0</span><br><span class="line">127.0.0.1:6379&gt; ZRANK myzset m2</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE myzset 0 1 # 获取索引在 0~1的成员</span><br><span class="line">1) &quot;m1&quot;</span><br><span class="line">2) &quot;m3&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE myzset 0 -1 # 获取全部成员</span><br><span class="line">1) &quot;m1&quot;</span><br><span class="line">) &quot;m3&quot;</span><br><span class="line">3) &quot;m2&quot;</span><br><span class="line"></span><br><span class="line">#testset&#x3D;&gt;&#123;abc,add,amaze,apple,back,java,redis&#125; score均为0</span><br><span class="line">------------------ZRANGEBYLEX---------------------------------</span><br><span class="line"># 区间用法 </span><br><span class="line">127.0.0.1:6379&gt; zadd testset 0 abc 0 add 0 amaze 0 apple 0 back 0 java 0 redis</span><br><span class="line">(integer) 7</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset - + # 返回所有成员 - +代表所有的区间</span><br><span class="line">1) &quot;abc&quot;</span><br><span class="line">2) &quot;add&quot;</span><br><span class="line">3) &quot;amaze&quot;</span><br><span class="line">4) &quot;apple&quot;</span><br><span class="line">5) &quot;back&quot;</span><br><span class="line">6) &quot;java&quot;</span><br><span class="line">7) &quot;redis&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset - + LIMIT 0 3 # 分页 按索引显示查询结果的 0,1,2条记录</span><br><span class="line">1) &quot;abc&quot;</span><br><span class="line">2) &quot;add&quot;</span><br><span class="line">3) &quot;amaze&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset - + LIMIT 3 3 # 显示 3,4,5条记录</span><br><span class="line">1) &quot;apple&quot;</span><br><span class="line">2) &quot;back&quot;</span><br><span class="line">3) &quot;java&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset (- [apple # 显示 (-,apple] 区间内的成员 就是展示apple及前面的数据</span><br><span class="line">1) &quot;abc&quot;</span><br><span class="line">2) &quot;add&quot;</span><br><span class="line">3) &quot;amaze&quot;</span><br><span class="line">4) &quot;apple&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset [apple [java # 显示 [apple,java]字典区间的成员</span><br><span class="line">1) &quot;apple&quot;</span><br><span class="line">2) &quot;back&quot;</span><br><span class="line">3) &quot;java&quot;</span><br><span class="line"># 其实就是左开右开 但是-代表所有所以( [都是一样的</span><br><span class="line">127.0.0.1:6379&gt; ZRANGEBYLEX testset (- (apple</span><br><span class="line">1) &quot;abc&quot;</span><br><span class="line">2) &quot;add&quot;</span><br><span class="line">3) &quot;amaze&quot;</span><br><span class="line"></span><br><span class="line">------------------ZREM--ZREMRANGEBYLEX--ZREMRANGBYRANK--ZREMRANGEBYSCORE--------------------------------</span><br><span class="line">127.0.0.1:6379&gt; ZREM testset abc # 移除成员abc</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; ZREMRANGEBYLEX testset [apple [java # 移除字典区间[apple,java]中的所有成员</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; ZREMRANGEBYRANK testset 0 1 # 移除排名0~1的所有成员</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; ZREMRANGEBYSCORE myzset 0 3 # 移除score在 [0,3]的成员</span><br><span class="line">(integer) 2</span><br><span class="line"></span><br><span class="line"># testset&#x3D;&gt; &#123;abc,add,apple,amaze,back,java,redis&#125; score均为0</span><br><span class="line"># myzset&#x3D;&gt; &#123;(m1,1),(m2,2),(m3,3),(m4,4),(m7,7),(m9,9)&#125;</span><br><span class="line">----------------ZREVRANGE--ZREVRANGEBYSCORE--ZREVRANGEBYLEX-----------</span><br><span class="line"># 先倒排序 再筛选</span><br><span class="line"># ZREVRANGE 就是zset reverse range 反转根据索引展示</span><br><span class="line">127.0.0.1:6379&gt; zadd myset 1 m1 2 m2 3 m3 4 m4 7 m7 9 m9</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE myset 0 -1</span><br><span class="line">1) &quot;m1&quot;</span><br><span class="line">2) &quot;m2&quot;</span><br><span class="line">3) &quot;m3&quot;</span><br><span class="line">4) &quot;m4&quot;</span><br><span class="line">5) &quot;m7&quot;</span><br><span class="line">6) &quot;m9&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGE myset 0 3 # 按score递减排序，然后按索引，返回结果的 0~3</span><br><span class="line">1) &quot;m9&quot;</span><br><span class="line">2) &quot;m7&quot;</span><br><span class="line">3) &quot;m4&quot;</span><br><span class="line">4) &quot;m3&quot;</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGE myset 2 4 # 返回排序结果的 索引的2~4</span><br><span class="line">1) &quot;m4&quot;</span><br><span class="line">2) &quot;m3&quot;</span><br><span class="line">3) &quot;m2&quot;</span><br><span class="line"></span><br><span class="line"># 按score递减顺序 返回集合中分数在[2,6]之间的成员</span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGEBYSCORE myset 6 2</span><br><span class="line">1) &quot;m4&quot;</span><br><span class="line">2) &quot;m3&quot;</span><br><span class="line">3) &quot;m2&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGEBYSCORE myset 2 6 </span><br><span class="line">(empty list or set) # 这样写是报错的 因为不存在</span><br><span class="line">127.0.0.1:6379&gt; ZREVRANGEBYLEX testset [java (add # 按字典倒序 返回集合中(add,java]字典区间的成员</span><br><span class="line">1) &quot;java&quot;</span><br><span class="line">2) &quot;back&quot;</span><br><span class="line">3) &quot;apple&quot;</span><br><span class="line">4) &quot;amaze&quot;</span><br><span class="line"></span><br><span class="line">-----------------------------补充---------------------------</span><br><span class="line"># 补充这个是自动排序的根据首字母 如果score最小就最前面 否则在后面</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE testset 0 -1</span><br><span class="line">1) &quot;java&quot;</span><br><span class="line">2) &quot;redis&quot;</span><br><span class="line"># 这边发现添加的两个都是score为0的根据首字母排序了</span><br><span class="line">127.0.0.1:6379&gt; ZADD testset 0 hehe 0 xixi</span><br><span class="line">(integer) 2</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE testset 0 -1</span><br><span class="line">1) &quot;hehe&quot;</span><br><span class="line">2) &quot;java&quot;</span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line">4) &quot;xixi&quot;</span><br><span class="line"># 然后设置了score为1的 aaa实在最后</span><br><span class="line">127.0.0.1:6379&gt; ZADD testset 1 aaa</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE testset 0 -1</span><br><span class="line">1) &quot;hehe&quot;</span><br><span class="line">2) &quot;java&quot;</span><br><span class="line">3) &quot;redis&quot;</span><br><span class="line">4) &quot;xixi&quot;</span><br><span class="line">5) &quot;aaa&quot;</span><br><span class="line">-------------------------ZREVRANK------------------------------</span><br><span class="line"># ZREVRANK 就是先倒排 再根据值找索引 位置</span><br><span class="line">127.0.0.1:6379&gt; ZREVRANK myset m7 # 按score递减顺序，返回成员m7索引</span><br><span class="line">(integer) 1</span><br><span class="line">127.0.0.1:6379&gt; ZREVRANK myset m2</span><br><span class="line">(integer) 4</span><br><span class="line"></span><br><span class="line"># mathscore&#x3D;&gt;&#123;(xm,90),(xh,95),(xg,87)&#125; 小明、小红、小刚的数学成绩</span><br><span class="line"># enscore&#x3D;&gt;&#123;(xm,70),(xh,93),(xg,90)&#125; 小明、小红、小刚的英语成绩</span><br><span class="line">-------------------ZINTERSTORE--ZUNIONSTORE---------------------------------</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE mathscore 0 -1</span><br><span class="line">1) &quot;xg&quot;</span><br><span class="line">2) &quot;xm&quot;</span><br><span class="line">3) &quot;xh&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE enscore 0 -1</span><br><span class="line">1) &quot;xm&quot;</span><br><span class="line">2) &quot;xg&quot;</span><br><span class="line">3) &quot;xh&quot;</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE enscore 0 -1 withscores</span><br><span class="line">1) &quot;xm&quot;</span><br><span class="line">2) &quot;70&quot;</span><br><span class="line">3) &quot;xg&quot;</span><br><span class="line">4) &quot;90&quot;</span><br><span class="line">5) &quot;xh&quot;</span><br><span class="line">6) &quot;93&quot;</span><br><span class="line"></span><br><span class="line"># 将mathscore enscore进行合并 结果存放到sumscore</span><br><span class="line">127.0.0.1:6379&gt; ZINTERSTORE sumscore 2 mathscore enscore</span><br><span class="line">(integer) 3</span><br><span class="line"># 合并后的score是之前集合中所有score的和</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE sumscore 0 -1 withscores</span><br><span class="line">1) &quot;xm&quot;</span><br><span class="line">2) &quot;160&quot;</span><br><span class="line">3) &quot;xg&quot;</span><br><span class="line">4) &quot;177&quot;</span><br><span class="line">5) &quot;xh&quot;</span><br><span class="line">6) &quot;188&quot;</span><br><span class="line"></span><br><span class="line"># 就是取 两个数据的最小的值展示 带score排序的</span><br><span class="line">127.0.0.1:6379&gt; ZUNIONSTORE lowestscore 2 mathscore enscore AGGREGATE MIN # 取两个集合的成员score最小值作为结果的</span><br><span class="line">(integer) 3</span><br><span class="line">127.0.0.1:6379&gt; ZRANGE lowestscore 0 -1 withscores</span><br><span class="line">1) &quot;xm&quot;</span><br><span class="line">2) &quot;70&quot;</span><br><span class="line">3) &quot;xg&quot;</span><br><span class="line">4) &quot;87&quot;</span><br><span class="line">5) &quot;xh&quot;</span><br><span class="line">6) &quot;93&quot;</span><br><span class="line"></span><br><span class="line">~~~</span><br><span class="line"></span><br><span class="line">其他API ，需要再去查</span><br><span class="line"></span><br><span class="line">案例思路：set 集合，班级成绩表，工资表排序！ </span><br><span class="line"></span><br><span class="line">普通消息，1  重要消息 ，2 带权重进行判断</span><br><span class="line"></span><br><span class="line">排行榜应用实现，取top10 </span><br><span class="line"></span><br><span class="line">----</span><br><span class="line"></span><br><span class="line"># 三种特殊数据类型</span><br><span class="line"></span><br><span class="line">这些在开源项目中有涉及，更有助于写出高效优美的代码</span><br><span class="line"></span><br><span class="line">## geospatial地理位置</span><br><span class="line"></span><br><span class="line">----</span><br><span class="line"></span><br><span class="line">朋友圈的定位，附近的人，打车距离计算</span><br><span class="line"></span><br><span class="line">Redis的Geo再  3.2  版本 就推出了，这个功能可以推算地理位置信息，两地之间的距离，方圆几里的人</span><br><span class="line"></span><br><span class="line">沈阳</span><br><span class="line"></span><br><span class="line">【经度】123.429096</span><br><span class="line"></span><br><span class="line">【纬度】41.796767</span><br><span class="line"></span><br><span class="line">![](https:&#x2F;&#x2F;gitee.com&#x2F;gitee-zct&#x2F;os&#x2F;raw&#x2F;master&#x2F;pic&#x2F;20210416-212650-0621.png)</span><br><span class="line"></span><br><span class="line">&gt; geoadd </span><br><span class="line">&gt;</span><br><span class="line">&gt; 规则：两级无法添加，我们一般会下载城市数据，直接通过Java一次性录入</span><br><span class="line">&gt;</span><br><span class="line">&gt; 参数： key    值（经度，维度，名称）</span><br><span class="line">&gt;</span><br><span class="line">&gt; &#96;&#96;&#96;js</span><br><span class="line">&gt; 127.0.0.1:6379&gt; geoadd china:city 116.40 39.90 beijin</span><br><span class="line">&gt; (integer) 1</span><br><span class="line">&gt; 127.0.0.1:6379&gt; geoadd china:city 121.47 31.23 shanghai</span><br><span class="line">&gt; (integer) 1</span><br><span class="line">&gt; 127.0.0.1:6379&gt; geoadd china:city 160.50 29.53 chongqing</span><br><span class="line">&gt; (integer) 1</span><br><span class="line">&gt; 127.0.0.1:6379&gt; geoadd china:city 114.05 22.52 shenzheng</span><br><span class="line">&gt; (integer) 1</span><br><span class="line">&gt; 127.0.0.1:6379&gt; geoadd china:city 120.16 30.24 hangzhou</span><br><span class="line">&gt; (integer) 1</span><br></pre></td></tr></table></figure>
<blockquote>
</blockquote>
<p><img src="https://gitee.com/gitee-zct/os/raw/master/pic/20210416-215459-0904.png"></p>
<blockquote>
<p>geopos </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; geopos china:city beijin  #获取指定的经度纬度</span><br><span class="line">&gt;<span class="number">1</span>) <span class="number">1</span>) <span class="string">&quot;116.39999896287918091&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="string">&quot;39.90000009167092543&quot;</span></span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>两人之间距离</p>
<p>单位： </p>
<p>​        m   米</p>
<p>​        km 千米</p>
<p>​        mi 英里 </p>
<p>​        ft   英尺</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; geodist china:city shanghai beijin   #</span><br><span class="line"><span class="string">&quot;1067378.7564&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; geodist china:city shanghai beijin km #产看北京上海直线距离</span><br><span class="line"><span class="string">&quot;1067.3788&quot;</span></span><br></pre></td></tr></table></figure>
</blockquote>
<hr>
<blockquote>
<p>查看附近的人？（获取所有附近的人位置，定位，）通过半径查询</p>
<p>所有数据应该录入数据库才会使结果精确</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; georadius china:city 110 30 1000 km  # 以100 30经纬度为中心，寻找方圆半径1000 km内的城市，</span><br><span class="line">&gt;<span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line"></span><br><span class="line">&gt;127.0.0.1:6379&gt; georadius china:city 110 30 1000 km withdist   #显示到中心距离</span><br><span class="line">&gt;<span class="number">1</span>) <span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="string">&quot;924.6408&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="number">1</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="string">&quot;977.5143&quot;</span></span><br><span class="line"></span><br><span class="line">&gt;127.0.0.1:6379&gt; georadius china:city 110 30 1000 km withcoord  #显示他人定位信息</span><br><span class="line">&gt;<span class="number">1</span>) <span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="number">1</span>) <span class="string">&quot;114.04999762773513794&quot;</span></span><br><span class="line">     <span class="number">2</span>) <span class="string">&quot;22.5200000879503861&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="number">1</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="number">1</span>) <span class="string">&quot;120.1600000262260437&quot;</span></span><br><span class="line">     <span class="number">2</span>) <span class="string">&quot;30.2400003229490224&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>获取指定数量的人</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; georadius china:city 110 30 1000 km withdist withcoord count 1  #指定数量，距离，经纬度</span><br><span class="line">&gt;<span class="number">1</span>) <span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">  <span class="number">2</span>) <span class="string">&quot;924.6408&quot;</span></span><br><span class="line">  <span class="number">3</span>) <span class="number">1</span>) <span class="string">&quot;114.04999762773513794&quot;</span></span><br><span class="line">     <span class="number">2</span>) <span class="string">&quot;22.5200000879503861&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; georadiusbymember china:city beijin 3000 km          #找出位于指定元素周围的其他元素</span><br><span class="line">&gt;<span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">&gt;<span class="number">3</span>) <span class="string">&quot;shanghai&quot;</span></span><br><span class="line">&gt;<span class="number">4</span>) <span class="string">&quot;beijin&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; geohash china:city beijin chongqing      #将二维的经纬度转换为一维的字符串，</span><br><span class="line">&gt;<span class="number">1</span>) <span class="string">&quot;wx4fbxxfke0&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="string">&quot;xt4purb89n0&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


</blockquote>
<hr>
<blockquote>
<p>geo底层的实现原理是zset ，我们可以使用zset命令来操作zeo</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&gt;127.0.0.1:6379&gt; zrange china:city 0 -1   #查看地图中全部的元素</span><br><span class="line">&gt;<span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">&gt;<span class="number">3</span>) <span class="string">&quot;shanghai&quot;</span></span><br><span class="line">&gt;<span class="number">4</span>) <span class="string">&quot;beijin&quot;</span></span><br><span class="line">&gt;<span class="number">5</span>) <span class="string">&quot;chongqing&quot;</span></span><br><span class="line">&gt;127.0.0.1:6379&gt; zrem china:city beijin  #移除指定元素</span><br><span class="line">&gt;(integer) <span class="number">1</span></span><br><span class="line">&gt;<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; zrange china:city <span class="number">0</span> -<span class="number">1</span></span><br><span class="line">&gt;<span class="number">1</span>) <span class="string">&quot;shenzheng&quot;</span></span><br><span class="line">&gt;<span class="number">2</span>) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">&gt;<span class="number">3</span>) <span class="string">&quot;shanghai&quot;</span></span><br><span class="line">&gt;<span class="number">4</span>) <span class="string">&quot;chongqing&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


</blockquote>
<h2 id="Hyperloglog"><a href="#Hyperloglog" class="headerlink" title="Hyperloglog"></a>Hyperloglog</h2><blockquote>
<p>什么是基数？</p>
</blockquote>
<p>A{1,3,5,7,9}</p>
<p>  B{1,3,5,7,8}</p>
<p>基数：不重复的元素的数量 6可以接受数量</p>
<blockquote>
<p>简介</p>
</blockquote>
<p>redis 2.8.9版本更新了Hyperloglog数据结构</p>
<p>Redis Hyperloglog 基数统计的算法</p>
<p><strong>优点</strong>：占的内存是固定的，2^64不同的元素的技术，只要浪费12KB内存，</p>
<h3 id="网站的UV-一个人访问多次，算作一次访问"><a href="#网站的UV-一个人访问多次，算作一次访问" class="headerlink" title="网站的UV      (一个人访问多次，算作一次访问)"></a>网站的UV      (一个人访问多次，算作一次访问)</h3><p>传统的方法;set方法保存元素的id,然后就可以统计set中的元素数量作为标准来判断</p>
<p>这个方法保存大量id，比较麻烦！我们的目的是计数，而不是保存id </p>
<p>0.81% 错误率！统计UV可以忽略  、、//如果不能错误可以使用mapreduce一个一个算  布隆过滤器？</p>
 <figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">测试基数统计（使用的位计算来实现的）</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; pfadd mykey a b c</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line">127.0.0.1:6379&gt; pfcount mykey   # 统计mykey元素的基数的数量</span><br><span class="line">(integer) <span class="number">3</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; pfadd mykey1 d e f</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line">127.0.0.1:6379&gt; pfcount mykey1 # 统计数量</span><br><span class="line">(integer) <span class="number">3</span></span><br><span class="line">127.0.0.1:6379&gt; pfmerge mykey2 mykey mykey1  # 统计合并两组mykey mykey=&gt;mykey2 并集</span><br><span class="line">OK</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; pfcount mykey2</span><br><span class="line">(integer) <span class="number">6</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Bitmaps"><a href="#Bitmaps" class="headerlink" title="Bitmaps"></a>Bitmaps</h2><blockquote>
<p>位运算</p>
</blockquote>
<p>统计用户信息，活跃，不活跃，登录，未登录，打卡！两个状态的使用bitmaps</p>
<p>bitmaps位图，数据结构，都是操作二进制位来进行记录，就只要0 和1 两个状态</p>
<p>365 天=365bit   1字节=8bit   46字节左右</p>
<blockquote>
<p>使用bitmap记录周一到周日的打卡</p>
<p>周 一:1           周二：0             周三：0…</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; setbit sign 0 1    #周一打卡了</span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line">127.0.0.1:6379&gt; setbit sign 1 0   #周二未打卡</span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; setbit sign <span class="number">2</span> <span class="number">0</span></span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; setbit sign <span class="number">3</span> <span class="number">1</span></span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; setbit sign <span class="number">4</span> <span class="number">1</span></span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; setbit sign <span class="number">5</span> <span class="number">0</span></span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>&gt; setbit sign <span class="number">6</span> <span class="number">1</span></span><br><span class="line">(integer) <span class="number">0</span></span><br></pre></td></tr></table></figure>

<p>查看某天是否打开</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; getbit sign 3  #查看周四是否打卡</span><br><span class="line">(integer) <span class="number">1</span></span><br><span class="line">127.0.0.1:6379&gt; getbit sign 5  #查看周六是否打卡</span><br><span class="line">(integer) <span class="number">0</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>统计操作：统计打卡的天数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; bitcount sign  #统计这周的打卡记录就可以看到是否有全勤</span><br><span class="line">(integer) <span class="number">4</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h1 id="事物"><a href="#事物" class="headerlink" title="事物"></a>事物</h1><hr>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">A原子性C一致性I隔离性D持久性</span><br><span class="line">要么同时成功，要么同时失败----原子性！在关系型数据库非常重要</span><br></pre></td></tr></table></figure>

<hr>
<p><font color='orange'>Redis的事务 ↓</font></p>
<p>Redis事物的本质，一组命令的集合！一个事物的所有命令都会被序列化，在事物执行过程中，会按照顺序执行</p>
<p>一次性，顺序性，排他性！执行一系列的命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">---队列  set set get 执行</span><br></pre></td></tr></table></figure>

<p>==redis事物没有隔离级别的概念！==</p>
<p>Redis所有的命令在事物中，并没有直接执行！只有在发起命令的时候才会执行！Exec</p>
<p>==Redis单条命令保持原子性，但是事物不保证原子性==</p>
<p>redis的事务：</p>
<ul>
<li>开启事务（multi）</li>
<li>命令入队  (…)</li>
<li>执行事务  (exec)</li>
</ul>
<p>锁：Redis可以实现乐观锁</p>
<blockquote>
<p>正常执行事务</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; MULTI            #开启事务</span><br><span class="line">OK</span><br><span class="line"># 命令入队</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; set k1 v1</span><br><span class="line">QUEUED</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; set k2 v2</span><br><span class="line">QUEUED</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; get k2</span><br><span class="line">QUEUED</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; set k3 v3</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; exec        #执行事务</span><br><span class="line"><span class="number">1</span>) OK</span><br><span class="line"><span class="number">2</span>) OK</span><br><span class="line"><span class="number">3</span>) <span class="string">&quot;v2&quot;</span></span><br><span class="line"><span class="number">4</span>) OK</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>放弃事务</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; MULTI       #开启事务</span><br><span class="line">OK</span><br><span class="line"># 事务入队</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; set k1 v1</span><br><span class="line">QUEUED</span><br><span class="line"><span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">6379</span>(TX)&gt; set k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; DISCARD  #取消事务</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; get k4       #事务队列中的命令都不会被执行</span><br><span class="line">(nil)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p><font color='cornflowerblue'>编译型异常（代码有错误！命令有错），事务中所有的命令都不会执行</font></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; MULTI </span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; flushdb</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; setget k1 v1</span><br><span class="line">(error) ERR unknown command &#96;setget&#96;, with args beginning with: &#96;k1&#96;, &#96;v1&#96;, </span><br><span class="line">127.0.0.1:6379(TX)&gt; set k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; get v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">(error) EXECABORT Transaction discarded because of previous errors.</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<blockquote>
<p><font color='cornflowerblue'>运行时异常（1/0）事务队列中存在语法错误，那么在执行命令的时候，其他命令可以正常执行</font></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; set k1 &quot;v1&quot;</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; incr k1</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; set k2 v2</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">1) (error) ERR value is not an integer or out of range</span><br><span class="line">2) OK</span><br><span class="line">127.0.0.1:6379&gt; get k2</span><br><span class="line">&quot;v2&quot;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p><font color='cornflowerblu'>监控！</font>面试常问</p>
</blockquote>
<p>悲观锁：</p>
<ul>
<li>什么时候都觉得会出问题，无论做什么都加锁，效率低下</li>
</ul>
<p>乐观锁:</p>
<ul>
<li>认为什么时候都不会出问题，所有不会上锁，更新数据时会去判断一下，再次期间是否有人修改过这个数据，</li>
<li>获取version</li>
<li>更新时比较</li>
</ul>
<blockquote>
<p><font color='green'>Redis的监视测试</font></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> money 100</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; <span class="built_in">set</span> out 0</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; watch money   <span class="comment">#监视 money 对象</span></span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; DECRby money 20  <span class="comment"># 事务正常结束，数据期间没有发生变动，这个时候正常执行</span></span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; INCRby out 20</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC</span><br><span class="line">1) (<span class="built_in">integer</span>) 80</span><br><span class="line">2) (<span class="built_in">integer</span>) 20</span><br></pre></td></tr></table></figure>

<hr>
<p><font color='orange'>测试多线程修改值，使用watch可以当作redis的乐观锁操作！</font></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; clear</span><br><span class="line">127.0.0.1:6379&gt; watch money</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379&gt; MULTI</span><br><span class="line">OK</span><br><span class="line">127.0.0.1:6379(TX)&gt; DECRby money 10</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; incrby out 10</span><br><span class="line">QUEUED</span><br><span class="line">127.0.0.1:6379(TX)&gt; EXEC    <span class="comment">#执行之前另外一个线程修改值，导致执行失败</span></span><br><span class="line">(nil)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>如果修改失败，获取最新的值</p>
</blockquote>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/zhu/tags/redis/" rel="tag"># redis</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/zhu/2021-04-16/http%E7%AC%94%E8%AE%B0%20-%20%E5%89%AF%E6%9C%AC/" rel="next" title="HTTP协议">
                <i class="fa fa-chevron-left"></i> HTTP协议
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/zhu/2021-04-18/redis(2)/" rel="prev" title="redis(2)">
                redis(2) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/zhu/archives/">
              
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/zhu/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/zhu/tags/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#redis"><span class="nav-number">1.</span> <span class="nav-text">redis</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Hyperloglog"><span class="nav-number">1.1.</span> <span class="nav-text">Hyperloglog</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BD%91%E7%AB%99%E7%9A%84UV-%E4%B8%80%E4%B8%AA%E4%BA%BA%E8%AE%BF%E9%97%AE%E5%A4%9A%E6%AC%A1%EF%BC%8C%E7%AE%97%E4%BD%9C%E4%B8%80%E6%AC%A1%E8%AE%BF%E9%97%AE"><span class="nav-number">1.1.1.</span> <span class="nav-text">网站的UV      (一个人访问多次，算作一次访问)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Bitmaps"><span class="nav-number">1.2.</span> <span class="nav-text">Bitmaps</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8B%E7%89%A9"><span class="nav-number">2.</span> <span class="nav-text">事物</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zhu</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">25.1k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





        
<div class="busuanzi-count">
 
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


  
    <span class="site-uv">
      踩过的小伙伴
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      本站访问量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/zhu/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/zhu/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/zhu/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/zhu/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/zhu/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/zhu/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/zhu/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/zhu/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/zhu/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/zhu/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/zhu/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/zhu/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/zhu/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "./public/search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/zhu/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('-1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
